import time

from selenium import webdriver
from selenium.webdriver.common.by import By
from unittest import TestCase
from ddt import ddt, data, unpack


@ddt
class TestLogin(TestCase):
    login_test_data = [
        ['lisi', '123456', '个人理财系统'],
        ['admin', '123456', '后台管理系统'],
        ['lisi3', '123456', '个人理财系统'],
        ['lisi', '1234567', '个人理财系统'],
        ['admin1', '123456', '后台管理系统'],
        ['admin', '1234567', '后台管理系统']
    ]

    @data(*login_test_data)
    @unpack
    def testLogin(self, username, password, expected):
        option = webdriver.ChromeOptions()
        option.add_argument('--headless')
        # 创建谷歌浏览器对象
        driver = webdriver.Chrome(options=option)

        # 窗口最大化
        driver.maximize_window()

        driver.get("http://localhost:90")

        # time.sleep(2)

        # 输入用户名
        driver.find_element(By.XPATH, "//input[@id='username']").send_keys(username)

        # time.sleep(1)
        # 输入密码
        driver.find_element(By.XPATH, "//input[@id='password']").send_keys(password)
        # time.sleep(1)

        # 点击登陆
        driver.find_element(By.XPATH, "//button[@id='login_btn']").click()

        time.sleep(1)

        # 获取实际结果
        # result = driver.find_element(By.TAG_NAME, 'title').text  # .text取出文本数据
        result = driver.title
        if result == '度小满理财-登录[内测版]':
            untip = driver.find_element(By.XPATH, '/html/body/div/div/div/form/div[1]/span[1]').text
            if untip == '用户名不存在':
                print('用户名不存在')
            else:
                print('密码错误')


        # if result == "个人理财系统":
        #     print("通过")
        # else:
        #     print("不通过！")
        #     driver.save_screenshot("lisi----123456.jpg")

        # time.sleep(2)

        # 退出浏览器
        driver.quit()
        self.assertEqual(result, expected)
